namespace CSharpPro1._1009;

public class Demo
{
    public void TestPrint()
    {
        int a = 10;
        for (int i = 1; i <= 10; i++)
        {
            if (i == 5)
            {
                //break 结束循环
                break;
                //continue 会跳过本次循环（跳过continue后的代码），继续之后的循环
                //continue;
            }
            
            Console.Write(i+"\t");
            
        }
    }

    //作业2
    //输出100以内的质数
    public void TestPrint2()
    {
        for (int i = 1; i <= 100; i++)
        {
            if (IsPrime(i))
            {
                Console.WriteLine(i);
            }
        }
    }
    //判断质数
    private bool IsPrime(int number)
    {
        if(number<=1)
            return false;
        if(number == 2)
            return true;
        if(number % 2 == 0)
            return false;
        for (int i = 3; i < number; i += 2)
        {
            if (number % i == 0)
                return false;
        }
        return true;
    }
    
    //作业1
    //水仙花数，不小于100且每位数的n次幂之和等于该数本身（n为该数位数），求1000以内的水仙花数
    public bool Is_narcissistic_number(int n)
    {
        if (n < 100 || n > 1000)
        {
            return false;
        }

        int num = 0;
        int temp = n;
        for (;temp > 0; temp /= 10)
        {
            num = num + 1;
        }
        int temp2 = n;
        int result = 0;
        for (; temp2 > 0; temp2 /= 10)
        {
            int digit = temp2 % 10;
            result = (int)Math.Pow(digit, num)+result;
            
        }
        return n == result;
    }
    //
}